const {
    rejects
} = require('assert')
const express = require('express')
const {
    where
} = require('sequelize/dist')

// let app = express()
const home = express.Router()

const shoplist = require('../db/model/shoplist')
// 获取post请求体数据
// app.use(express.urlencoded({
//     extended: false
// }))

home.get('/aa', (req, res) => {
    res.send('这是啊啊啊')
})
// 增加数据
home.post('/addmobile', async (req, res) => {
    console.log("14:", req.body)
    const {
        mobilename,
        mobileprice,
        mobilecolor,
        mobilesize,
        mobilearea,
        mobileshopname
    } = req.body

    let model = await shoplist.findOne({
        where: {
            mobile_name: mobilename
        }
    })
    // console.log(model);
    if (model == null) {
        let addshoplist = await shoplist.create({
            mobile_name: mobilename,
            mobile_price: mobileprice,
            mobile_color: mobilecolor,
            mobile_size: mobilesize,
            mobile_area: mobilearea,
            mobile_shopname: mobileshopname
        })
        res.send({
            code: 200,
            message: "插入成功"
        })
    } else {
        console.log("商品名称重复");
        res.send({
            code: 500,
            message: "商品名称重复"
        })
    }
})
// 查询列表分页
home.get('/showmobile/:current/:size', async (req, res) => {
    console.log("分页", req.params);
    // current是起始值，size是每页显示数据
    const {
        current,
        size
    } = req.params
    // count===>符合条件的查询总数，rows是查到的数据，是个数组
    let {
        count,
        rows
    } = await shoplist.findAndCountAll({
        attributes: ['id', 'mobile_name', 'mobile_price', 'mobile_color', 'mobile_size', 'mobile_area', 'mobile_shopname'],
        where: {
            udel: "Y"
        },
        // 1页的个数
        limit: parseInt(size),
        // 起始值
        offset: parseInt((current - 1) * size)
    })
    // console.log(count);
    // console.log(rows);
    res.send({
        count,
        rows,
        code: 200
    })
})
// 查询详情
home.get('/showmobiledetail', async (req, res) => {
    console.log(req.query);
    const {
        phoneid
    } = req.query

    let model = await shoplist.findOne({
        attributes: ['id', 'mobile_name', 'mobile_price', 'mobile_color', 'mobile_size', 'mobile_area', 'mobile_shopname'],
        where: {
            id: phoneid
        }
    })
    // console.log(model);
    res.send({
        model
    })

})
// 删除数据
home.delete('/delmobile/:id', async (req, res) => {
    console.log(req.params);
    const {
        id
    } = req.params
    let model = await shoplist.update({
        udel: "N"
    }, {
        where: {
            id
        }
    })
    res.send({
        code: 200,
        message: "删除成功"
    })
})
// 修改手机数据
home.post('/updatemobile', async (req, res) => {
    console.log(req.body);
    const {
        id,
        mobile_name,
        mobile_price,
        mobile_color,
        mobile_size,
        mobile_area,
        mobile_shopname
    } = req.body
    let model = await shoplist.update({
        mobile_name,
        mobile_price,
        mobile_color,
        mobile_size,
        mobile_area,
        mobile_shopname
    }, {where:{id}})
    res.send({code:200,message:"修改成功"})
})





module.exports = home